package template.page.user;

import java.util.List;

import org.apache.click.ActionListener;
import org.apache.click.Control;
import org.apache.click.control.Column;
import org.apache.click.control.Table;
import org.apache.click.dataprovider.DataProvider;

import template.page.BorderPage;

@SuppressWarnings("serial")
public class GameList extends BorderPage {

	public String title = "All Games";
	private Table gameTable = new Table("gameTable");

	@SuppressWarnings("rawtypes")
	public GameList() {
		addControl(gameTable);

		// Setup table
		gameTable.setClass(Table.CLASS_ITS);
		gameTable.setPageSize(25);
		gameTable.setShowBanner(true);
		gameTable.setSortable(true);

		gameTable.addColumn(new Column("id"));
		gameTable.addColumn(new Column("name"));
		gameTable.addColumn(new Column("minPlayer"));
		gameTable.addColumn(new Column("maxPlayer"));
		gameTable.addColumn(new Column("duration"));

		// Table rowList will be populated through a DataProvider which loads
		// data on demand.
		gameTable.setDataProvider(new DataProvider() {

			public List getData() {
				return getGameSessionService().getAllGames();
			}
		});

		// Below we setup the table to preserve it's state (sorting and paging)
		// while editing customers
		gameTable.getControlLink().setActionListener(new ActionListener() {

			public boolean onAction(Control source) {
				// Save Table sort and paging state between requests.
				// NOTE: we set the listener on the table's Link control which
				// is invoked
				// when the Link is clicked, such as when paging or sorting.
				// This ensures the table state is only saved when the state
				// changes, and
				// cuts down on unnecessary session replication in a cluster
				// environment.
				gameTable.saveState(getContext());
				return true;
			}
		});

		// Restore the table sort and paging state from the session between
		// requests
		gameTable.restoreState(getContext());
	}
}
